package com.yx.yinshanjinniu.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.base.BaseRVHolder;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.bean.UserTicketBean;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.GlideUtils;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.model.GoodsItemBean;
import com.zc.mychart.utils.FormatUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MarketGoodsAdapter extends BaseRVAdapter<GoodsItemBean> {
    private GoodsAllBean mGroupBean;
    private int mPosition;

    public MarketGoodsAdapter(Context context, List<GoodsItemBean> list) {
        super(context, list);
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_market_goods_info;
    }

    @Override
    public void onBindView(BaseRVHolder holder, final int position) {
        RelativeLayout rl_choose = holder.getViewHolder().get(R.id.rl_choose);
        ImageView iv_goods_image = holder.getViewHolder().get(R.id.iv_goods_img);
        TextView tv_goods_name = holder.getViewHolder().get(R.id.tv_goods_name);
        TextView tv_weight = holder.getViewHolder().get(R.id.tv_weight);
        ImageView iv_header = holder.getViewHolder().get(R.id.iv_header);

        if (mPosition == position) {
            rl_choose.setVisibility(View.VISIBLE);
        }else {
            rl_choose.setVisibility(View.GONE);
        }

        final GoodsItemBean bean = mList.get(position);
        GlideUtils.showImageRoundOptions(mContext, bean.product_img, iv_goods_image, 12);
        tv_goods_name.setText(bean.pro_name);
        tv_weight.setText(FormatUtil.formatDouble4(bean.unit_price));

        if(null != mGroupBean && mGroupBean.during_type == 0){
            iv_header.setVisibility(View.VISIBLE);
            iv_header.setImageResource(R.drawable.icon_home_xiu);
        }else{
            setTicketInfo(bean,iv_header);
        }

    }


    private Map<String, Integer> ticketMap = new HashMap<>();

    public void setTicketInfo(GoodsItemBean proInfoItemBean,ImageView iv_header) {
        if(DataUtils.isLogin(mContext)){
            ticketMap.clear();
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            if (bean == null) return;
            List<UserTicketBean> list = bean.ticket;
            if (list != null) {
                for (UserTicketBean ticket : list) {
                    ticketMap.put(ticket.goods_id, ticket.count);
                }
            }
            Integer ticket = 0;
            ticket = ticketMap.get(proInfoItemBean.goods_id);
            if(null != ticket && ticket > 0){
                iv_header.setVisibility(View.VISIBLE);
                iv_header.setImageResource(R.drawable.icon_home_ticket);
            }else{
                iv_header.setVisibility(View.GONE);
            }
        }
    }

    public void setProGroupBean(GoodsAllBean bean){
        this.mGroupBean = bean;
        notifyDataSetChanged();
    }


    public void setPosition(int p) {
        this.mPosition = p;
        notifyDataSetChanged();
    }

    public int getPosition(){
        return  mPosition;
    }

}
